/*******************************************************************************
																				
	DESCRIPTION: This file trains the expanded linear models.
	
*******************************************************************************/
 
clear all
global id_code 116

foreach vars in _ {
		
 	foreach model in ///
			EX_FullSample_OC EX_FullSample_UM EX_FullSample_UI  ///
			EX_FullSample_RR EX_FullSample_WE EX_FullSample_IQ  EX_FullSample_ALL ///
		{
		
		foreach year in 2006 {

		*******************************************************************************
		 * Keep only data used for parametre tuning, training and creating weights
		*******************************************************************************
		
		use "${data}/002_DataForR_`model'`vars'`year'.dta", clear
		
		* by keeping only those that appear in DataForR, but not those in PredEnsemble we keep observations that were used for parameter tuning, training etc. in the ML models
		merge 1:1 n using "${data}/003_MainWithEnsemblePred_`model'`vars'`year'.dta"
		keep if _merge==1
		drop _merge
		
		*******************************************************************************
		 * Make and save predictions
		*******************************************************************************
		
		foreach months in 6 {
			
			foreach unempl in 0 {
				
				local stand_vars "$demoEdu $migHist $incIndiv $incOther $incHist $indu $emplHist"
				
				if "`model'"=="EX_FullSample_WE" {
				local varSet "`stand_vars' $wealth"
				}	
				
				if "`model'"=="EX_FullSample_OC" {
				local varSet "`stand_vars' $occup"
				}

				if "`model'"=="EX_FullSample_RR" {
				local varSet "`stand_vars' $levelUI"
				}
				
				if "`model'"=="EX_FullSample_IQ" {
				local varSet "`stand_vars' $IQ"
				}
				
				if "`model'"=="EX_FullSample_UM" {
				local varSet "`stand_vars' $union"
				}
				
				if "`model'"=="EX_FullSample_UI" {
				local varSet "`stand_vars' $UI"
				}
				
				if "`model'"=="EX_FullSample_ALL"   {
				local varSet "`stand_vars' $union $IQ $levelUI $UI $wealth $occup"
				}		

			reg emplAft`months'M_`unempl'M_In `varSet'
			estimates store `model'`vars'
			
			}
		}

		*******************************************************************************
		 * Clean and prepare the part of data for which we make predictions
		*******************************************************************************
		use "${data}/002_DataForR_`model'`vars'`year'.dta", clear
		
		* by keeping only those that appear in DataForR and in PredEnsemble we keep observations that were in the holdout sample
		merge 1:1 n using "${data}/003_MainWithEnsemblePred_`model'`vars'`year'.dta"
		keep if _merge==3
		drop _merge
		
		gen DataYear = `year'

		*******************************************************************************
		 * Make predictions
		*******************************************************************************

		foreach months in 6 {
			foreach unempl in 0 {
				
				estimates restore `model'`vars'
				pause
				predict p_`model'`vars'`year'

				*******************************************************************************
				 * Get graphs and startistics for predictions using linear model
				*******************************************************************************									
				
				* Calculate mean of the empirical JFRs in a year
				egen mean_e_Aft`months'M_`unempl'M_In_`vars'`year' = 		///
					mean(emplAft`months'M_`unempl'M_In) if `year'==year(startU + `unempl'*30)

				
				* R-squared of outcome variable on predictions
				reg emplAft`months'M_`unempl'M_In p_`model'`vars'`year' if `year'==year(startU + `unempl'*30)
				gen R_`vars'_`year'=e(r2)

			}

		}
			
		keep mean* R*
		drop if R_`vars'_`year'==. | mean_e_Aft6M_0M_In_`vars'`year'==.
		keep if _n==1
		gen model = "`model'"
		gen vars = "`vars'"
		gen year = "`year'"
		 
		save "${data}/${id_code}_Linear_`model'`vars'`year'.dta", replace
		 	 
		
		}
	}
 }

 
* Combine all the stats into one file	
foreach year in 2006 { 

use "${data}/${id_code}_Linear_EX_FullSample_OC_`year'.dta", clear

	foreach model in EX_FullSample_OC EX_FullSample_UM EX_FullSample_UI  ///
		EX_FullSample_RR EX_FullSample_WE EX_FullSample_IQ  EX_FullSample_ALL ///
		{
	
	append using "${data}/${id_code}_Linear_`model'_`year'.dta"
		
	}
	
save "${data}/${id_code}_Linear_`year'_allExpandedModels.dta", replace

}
